package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Order;
import pojo.User;

import java.util.List;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 分页查询服务(带条件)
     *
     * @param pageNum 当前页码
     * @param order
     * @return
     */
    public PageInfo<User> queryByPage(int pageNum, Order order) {
        //每页记录数量
        int pageSize = 4;
        PageHelper.startPage(pageNum, pageSize);
        //创建分页信息对象pageInfo,构造器里需求查询结果，进行分页查询
        List<Order> orders;
        //无条件查询
        if (order == null) {
            orders = orderMapper.selectAll();

            //有条件查询
        } else {
            orders = orderMapper.selectByConditions(order);
        }

        //翻页信息对象
        PageInfo pageInfo = new PageInfo(orders);
        return pageInfo;
    }


    /**
     * 删除订单
     *
     * @param orderId
     * @return
     */
    public boolean removeOrder(String orderId) {
        int rs = orderMapper.deleteById(orderId);
        //删除了一条数据
        if (rs == 1) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 添加订单
     *
     * @param order
     * @return
     */
    public boolean addOrder(Order order) {
        int rs = orderMapper.insert(order);
        //插入成功
        if (rs == 1) {
            return true;

            //插入失败
        } else {
            return false;
        }
    }


    /**
     * 修改订单
     *
     * @param order
     * @return
     */
    public boolean modifyOrder(Order order) {
        int rs = orderMapper.update(order);
        //修改成功
        if (rs == 1) {
            return true;

            //修改失败
        } else {
            return false;
        }
    }


    /**
     * 根据orderId查询订单信息
     *
     * @param orderId
     * @return
     */
    public Order queryOrder(String orderId) {
        return orderMapper.selectById(orderId);
    }

}
